package com.tude.egarticle3.controller;

import com.tude.egarticle3.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Api(tags = "文件上传")
@Slf4j
@RestController
public class UploadController {
    @ApiOperation("文件上传")
    @PostMapping("/v1/upload")
    public JsonResult upload(MultipartFile file) throws IOException {

        String fileName = file.getOriginalFilename();
        String surfix = fileName.substring(fileName.lastIndexOf("."));

        fileName = UUID.randomUUID() + surfix;

        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());

        String dirPath = "C:/files";

        File staticFile = new File(dirPath + datePath);
        if (!staticFile.exists()){
            staticFile.mkdirs();
        }

        file.transferTo(new File(dirPath + datePath + fileName));

        return JsonResult.success(datePath + fileName);
    }
}
